package com.lge.clock.worldclock;

import java.io.IOException;

import com.lge.clock.util.Log;
import com.lge.clock.worldclock.cities.CityDBManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class WorldClockReceiver extends BroadcastReceiver {
	private static final String TAG = "WorldClockReceiver";
	private CityDBManager mCityManager = null;
	
	@Override
	public void onReceive(Context mContext, Intent intent) {
		String mAction = intent.getAction();
		
		Log.d(TAG, "====action==>" + mAction );
		
		if (mAction.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
			try {
				mCityManager = CityDBManager.getInstance(mContext);
				mCityManager.updateDST();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				Intent mIntent = new Intent("dstmanualupdate");
				mContext.sendBroadcast(mIntent);		
			}
			return;
		}
		
		if (mAction.equalsIgnoreCase("com.lge.clock.CHECK_DST_UPDATE")) {
			try {
				mCityManager = CityDBManager.getInstance(mContext);
				mCityManager.updateDST();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				Intent mIntent = new Intent("dstmanualupdate");
				mContext.sendBroadcast(mIntent);		
			}
			return;
		}
		
		if (mAction.equals(Intent.ACTION_TIME_CHANGED) 
			|| mAction.equals(Intent.ACTION_TIMEZONE_CHANGED)){
			
			try {
				mCityManager = CityDBManager.getInstance(mContext);
				mCityManager.updateDST();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				Intent mIntent = new Intent("dstmanualupdate");
				mContext.sendBroadcast(mIntent);
			}
			return;
		}
	}
}